﻿#ifndef _DATABASE_VECTOR_SOURCE_IMPL_H_H_
#define _DATABASE_VECTOR_SOURCE_IMPL_H_H_

#include "udatabase/dll_cfg.h"
#include "udatabase/geodb/vectorfeatureclass.h"
#include "udatabase/geodb/databasevectorsource.h"
#include "udatabase/sqldatabase.h"
#include "udatabase/geodb/vectorfeatureclassoperator_dbimp.h"
#include "udatabase/interface/ifields.h"
#include "udatabase/interface/igeometryfield.h"

namespace FD
{
    namespace Utility
    {
        namespace DB
        {
			class CSqlstandardization;
        }
    }
}


namespace FD{
	namespace Utility{
		namespace DB{

class CDatabaseVectorSourceImpl
{
public:
	CDatabaseVectorSourceImpl(FD::Utility::DB::CDatabaseVectorSource *ds);
	~CDatabaseVectorSourceImpl();
	/// <summary>
	/// 判断数据源是否有效
	/// </summary>
	/// <param name="name">指定名称</param>
	/// <returns>有效删除，返回true;否则，返回false</returns>
	bool isValid();
	/// <summary>
	/// 开始编辑，用于标识数据集处在编辑状态，并根据参数，设置相关编辑对象。
	/// </summary>
	/// <param name="withUndo">确定是否开启Undo功能</param>
	/// <returns>开启编辑成功，返回true；否则，返回false</returns>
	bool beginEditing(const String &Name); 
	/// <summary>
	/// 结束编辑，用于标识数据集结束编辑，根据参数，确实是否保存数据。
	/// </summary>
	/// <param name="isSave">需要保存编辑数据，设置true;否则，设置false</param>
	/// <returns>结束编辑成功，返回true;否则，返回false</returns>
	bool endEditing(const String &Name,bool isSave);

	bool IsEditing() const;
	/// <summary>
	/// 获取数据集类型
	/// </summary>
	/// <param name="name">指定名称</param>
	/// <returns>数据集类型</returns>
	FD::Utility::DB::Interface::DatasetType getDatasetType(const String &name);
	/// <summary>
	/// 删除指定数据集的Operator
	/// </summary>
	/// <param name="name">指定名称</param>
	/// <returns>对应的Operator</returns>
	FD::Utility::DB::Interface::IDatasetOperator* getOperatorRef(FD::Utility::DB::Interface::DatasetType type);
	/// <summary>
	/// 是否支持指定类型的数据集
	/// </summary>
	/// <param name="name">指定名称</param>
	/// <returns>成功删除，返回true;否则，返回false</returns>
	bool IsSupportedDataset(FD::Utility::DB::Interface::DatasetType type)
	{
		return m_oprs.find(type)==m_oprs.end() ? false : true;
	}
	/// <summary>
	/// 打开指定数据集名称的数据集
	/// </summary>
	/// <param name="name">指定名称</param>
	/// <returns>数据集对象指针，如果名字不存在，这返回null</returns>
	FD::Utility::DB::Interface::IDataset * openDataset(const String& name);
	/// <summary>
	/// 删除指定数据集名称的数据集
	/// </summary>
	/// <param name="name">指定名称</param>
	/// <returns>成功删除，返回true;否则，返回false</returns>
	bool deleteDataset(const String& name);
	/// <summary>
	/// 打开FeatureClass数据集
	/// </summary>
	/// <param name=""></param>
	/// <returns>CVectorFeatureClass的指针</returns>
	FD::Utility::DB::CVectorFeatureClass* openFeatureClass(const String &name);
	/// <summary>
	/// 创建FeatureClass数据集
	/// </summary>
	/// <param name=""></param>
	/// <returns>CVectorFeatureClass的指针</returns>
	bool createFeatureClass(const String &name,
								FD::Utility::DB::Interface::IGeometryField &geofield,
								const FD::Utility::DB::Interface::IFields &fields,
								const String &parent="NULL");
	/// <summary>
	/// 删除FeatureClass数据集
	/// </summary>
	/// <param name=""></param>
	/// <returns>成功删除，返回true;否则，返回false</returns>
	bool	deleteFeatureClass(const String &name);
	/// <summary>
	/// 获取网络数据集的创建者
	/// </summary>
	/// <param name=""></param>
	/// <returns>成功，返回指针;否则，返回NULL</returns>
	//FD::Utility::DB::CNetworkDatasetBuilder* getNetworkDatasetBuilder();
	/// <summary>
	/// 打开网络数据集
	/// </summary>
	/// <param name=""></param>
	/// <returns>成功，返回有效指针;否则，返回NULL</returns>
	//FD::Utility::DB::CNetworkDataset* openNetworkDataset(const String &name);
	/// <summary>
	/// 删除网络数据集
	/// </summary>
	/// <param name=""></param>
	/// <returns>成功，返回true;否则，返回False</returns>
	bool deleteNetworkDataset(const String &name);
	/// <summary>
	/// 获取标注数据集的创建者
	/// </summary>
	/// <param name=""></param>
	/// <returns>成功，返回指针;否则，返回NULL</returns>
	//FD::Utility::DB::CAnnotationBuilder* getAnnoDatasetBuilder();
	/// <summary>
	/// 打开标注数据集
	/// </summary>
	/// <param name=""></param>
	/// <returns>成功，返回有效指针;否则，返回NULL</returns>
	//FD::Utility::DB::CAnnoFeatureClass* openAnnoDataset(const String &name);
	/// <summary>
	/// 获取指定数据集类型的数据元信息
	/// </summary>
	/// <param name=""></param>
	/// <returns>数据元信息集合对象</returns>
	FD::Utility::DB::Interface::IDataMetaInfo* getDatasetInfo(const String &name);
	/// <summary>
	/// 获取指定数据集类型的数据元信息
	/// </summary>
	/// <param name=""></param>
	/// <returns>数据元信息集合对象</returns>
	FD::Utility::DB::Interface::IDataMetaInfos* getDatasetInfos(FD::Utility::DB::Interface::DatasetType type);
	/// <summary>
	/// 获取指定数据集类型的数据集
	/// </summary>
	/// <param name=""></param>
	/// <returns>数据集的迭代器</returns>
	FD::Utility::DB::Interface::IDatasetIterator* getDatasets(FD::Utility::DB::Interface::DatasetType type);
	/// <summary>
	/// 序列化字符串
	/// </summary>
	/// <param name=""></param>
	/// <returns></returns>
	String toXML() const;
	/// <summary>
	/// 获取数据源链接字符串
	/// </summary>
	/// <param name=""></param>
	/// <returns>链接字符串</returns>
	String getConnectionString() const;
	/// <summary>
	/// 初始化支持数据集的Operator
	/// </summary>
	/// <param name=""></param>
	/// <returns>链接字符串</returns>
	void initializeOperators();
	
private:
	/// <summary>
	/// 是否包含数据集
	/// </summary>
	/// <param name="name">指定名称</param>
	/// <returns>成功删除，返回true;否则，返回false</returns>
	bool  containDataset(const String &name);
	int32 createTable(const String& sql);
	int32 createGeometryTableTable(const char* datasetName);
	int32 getFieldMaxId(FD::Utility::DB::CSqlQuery& query, const String& sql);
	//创建数据集失败时用作删除操作，某些数据库，DDL类型语句无法回滚
	bool deleteFeatureClass2(const String &name);
	//执行多条sql语句
	bool executeMultiSQL(const String& sql);
private:
	//data segment
	bool					m_bValid;  
	bool					m_IsEditing;
	String					m_EditingName;
	uint32					m_EditingNum;	
	FD::Utility::DB::CDatabaseVectorSource	*m_pDataSource;
	map<FD::Utility::DB::Interface::DatasetType,FD::Utility::DB::Interface::IDatasetOperator*> m_oprs;
	FD::Utility::DB::CSqlstandardization		*sqlStd;
	
};


}}} // End of namespaces

#endif //_DATABASE_VECTOR_SOURCE_IMPL_H_H_


